package server.business.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import server.business.mapper.*;
import server.business.pojo.BloodTest;
import server.business.pojo.BloodTestReview;
import server.business.pojo.BloodTestVo;
import server.business.service.BloodSamplePatientService;
import server.business.service.BloodTestReviewService;
import server.business.service.BloodTestService;
import server.business.service.PatientService;
import server.business.utils.PageResult;
import server.business.utils.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/bloodTestReview")
public class BloodTestReviewController {
    //将service注入进来
    @Autowired
    private BloodTestService bloodTestService;
    @Autowired
    private BloodTestReviewMapper bloodTestReviewMapper;
    @Autowired
    BloodTestReviewService bloodTestReviewService;



    /**
     * 获取页面
     * 验血审核分页查询
     */
    @RequestMapping("/review/listByPage")
    @ResponseBody
    public PageResult getPagBloodTestReview(HttpServletRequest request, HttpServletResponse response) {
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        Page<BloodTestVo> page = new Page<>(currentPage,pageSize);
        QueryWrapper<BloodTestVo> queryWrapper=new QueryWrapper<>();
        IPage<BloodTestVo> samplePage = bloodTestReviewMapper.selectBloodTestReviewByPage(page,queryWrapper);
        PageResult pageResult = new PageResult("200", "成功", samplePage.getRecords(), currentPage, pageSize,
                samplePage.getTotal(), samplePage.getPages());
        List<BloodTestVo> bloodTestVo=page.getRecords();
        return pageResult;
    }

    /**
     * 审核
     * @param bloodTestVo
     * @return
     */
    @RequestMapping("/review/review")
    @ResponseBody
    public Result reviewBloodTest(@RequestBody BloodTestVo bloodTestVo) {
        //审核登记
        BloodTestReview bloodTestReview=bloodTestReviewMapper.selectByBloodTestId(bloodTestVo.getId());
        bloodTestReview.setReviewerId(bloodTestVo.getReviewerNumber());
        bloodTestReview.setReviewTime(bloodTestVo.getReviewTime());
        bloodTestReview.setRejectReason(bloodTestVo.getRejectReason());
        bloodTestReview.setReviewTime(new Date());
        bloodTestVo.setReviewTime(new Date());
//        System.out.println(bloodTest.getId());
        Result result = new Result();
        if(bloodTestVo.getState().equals("驳回")){
            bloodTestVo.setState("4");
            bloodTestReview.setState("3");
        }else if(bloodTestVo.getState().equals("审核通过")){
            bloodTestVo.setState("3");
            bloodTestReview.setState("2");
        }
        bloodTestReviewService.updateById(bloodTestReview);

        //拷贝成BloodTest
        BloodTest bloodTest=new BloodTest();
        BeanUtils.copyProperties(bloodTestVo,bloodTest);
        boolean success = bloodTestService.updateById(bloodTest);

        if(success) {
            result.setCode("200");
            result.setMsg("审核成功");
        }else{
            result.setCode("500");
            result.setMsg("审核失败");
        }
        return result;
    }

    /**
     * 验血审核搜索
     *
     * @param request  请求
     * @param response 响应
     * @return {@link PageResult}
     */
    @RequestMapping("/review/reviewSearch")
    @ResponseBody
    public PageResult bloodTestReviewSearch(HttpServletRequest request, HttpServletResponse response){
        //前端获取的检验单号、patientID,状态
        String inspectionSheet = request.getParameter("inspectionSheet");
        String medicalRecordNumber = request.getParameter("medicalRecordNumber");
        String bloodSampleNumber=request.getParameter("bloodSampleNumber");
        String name = request.getParameter("name");
        String reviewerName = request.getParameter("reviewerName");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String reviewState = request.getParameter("reviewState");
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));

        //创建分页对象
        Page<BloodTestVo> page = new Page<>(currentPage, pageSize);
        //创建查询条件
        QueryWrapper<BloodTestVo> queryWrapper = new QueryWrapper<>();
        if (inspectionSheet != "")
            queryWrapper.like("inspection_sheet", inspectionSheet);

        if (medicalRecordNumber != ""){
            queryWrapper.eq("medical_record_number", medicalRecordNumber);
        }
        if (bloodSampleNumber != "")
            queryWrapper.eq("blood_sample_number", bloodSampleNumber);
        if (name != "")
            queryWrapper.like("name", name);
        if (reviewerName != "")
            queryWrapper.like("reviewer_name", reviewerName);
        if(startTime != "")
            queryWrapper.ge("review_time", startTime);
        if(endTime != "")
            queryWrapper.le("review_time", endTime);
        if (reviewState != "")
            queryWrapper.eq("d.state",reviewState);
        //查询数据库
        bloodTestReviewMapper.selectBloodTestReviewByPage(page, queryWrapper);
        //返回的数据
        PageResult sampleDataVo = new PageResult("200", "成功", page.getRecords(), currentPage, pageSize, page.getTotal(), page.getPages());
        System.out.println(page.getTotal());
        sampleDataVo.setCode("200");
        sampleDataVo.setMsg("搜索成功");
        return sampleDataVo;
    }
}
